<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
      </template>
      <template #products="{ record }">
        <a-tag style="margin: 5px;" color="green" v-for="(item, index) in record.products" :key="index">{{ item.name
          }}</a-tag>
      </template>
      <template #inputs="{ record }">
        <a-tag style="margin: 5px;" color="blue" v-for="(item, index) in record.inputUses" :key="index">{{
      item.inputId_dictText }}</a-tag>
      </template>
      <template #dosage="{ record }">
        <a-tag style="margin: 5px;" color="cyan" v-for="(item, index) in record.inputUses" :key="index">{{ item.dosage
          }}KG</a-tag>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{ text }">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small"
          @click="downloadFile(text)">下载</a-button>
      </template>
      <template #img="{ text }">
        <TableImg :size="60" :simpleShow="true" :imgList="text" />
      </template>
      <template #video="{ text }">
        <!-- <video v-if="!!text" :src="text[0]" style="height: 54px;"></video> -->
        <div v-if="!!text" class="jeecg-basic-table-img flex items-center mx-auto" style="height: 60px; width: 60px;">
          <span class="ant-badge">
            <div class="img-div">
              <div class="ant-image" style="width: 60px;">
                <video class="ant-image-img" v-if="!!text" :src="text[0]" style="height: 54px;"></video>
                <div class="ant-image-mask">
                  <div class="ant-image-mask-info" @click="previewVideo(text)"><span role="img" aria-label="eye"
                      class="anticon anticon-eye"><svg focusable="false" class="" data-icon="eye" width="1em"
                        height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896">
                        <path
                          d="M942.2 486.2C847.4 286.5 704.1 186 512 186c-192.2 0-335.4 100.5-430.2 300.3a60.3 60.3 0 000 51.5C176.6 737.5 319.9 838 512 838c192.2 0 335.4-100.5 430.2-300.3 7.7-16.2 7.7-35 0-51.5zM512 766c-161.3 0-279.4-81.8-362.7-254C232.6 339.8 350.7 258 512 258c161.3 0 279.4 81.8 362.7 254C791.5 684.2 673.4 766 512 766zm-4-430c-97.2 0-176 78.8-176 176s78.8 176 176 176 176-78.8 176-176-78.8-176-176-176zm0 288c-61.9 0-112-50.1-112-112s50.1-112 112-112 112 50.1 112 112-50.1 112-112 112z">
                        </path>
                      </svg></span>预览</div>
                </div>
              </div>
            </div>
            <sup v-if="text.length" data-show="true" class="ant-scroll-number ant-badge-count"
              :title="text.length"><span class="ant-scroll-number-only" style="transition: none 0s ease 0s;">
                <p class="ant-scroll-number-only-unit current">{{ text.length }}</p>
              </span></sup>
          </span>
        </div>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <TOperationRecordsModal @register="registerModal" @success="handleSuccess"></TOperationRecordsModal>
    <Desc @register="registerDesc"></Desc>
    <PreviewVideo @register="registerVideo" @success="handleOk"></PreviewVideo>
  </div>
</template>

<script lang="ts" name="TOperationRecords-tOperationRecords" setup>
import { ref, computed, unref } from 'vue';
import { BasicTable, useTable, TableAction, TableImg } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useListPage } from '/@/hooks/system/useListPage'
import TOperationRecordsModal from './components/TOperationRecordsModal.vue'
import { columns, searchFormSchema } from './TOperationRecords.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, repush } from './TOperationRecords.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import Desc from "./components/Desc.vue";
import PreviewVideo from "./components/PreviewVideo.vue";
const [registerDesc, { openModal: openDescInfo }] = useModal();
const [registerVideo, { openModal: openVideo }] = useModal();
const checkedKeys = ref<Array<string | number>>([]);
//注册model
const [registerModal, { openModal }] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '操作记录',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [
      ],
      fieldMapToTime: [
      ],
    },
    actionColumn: {
      width: 120,
      fixed: 'right'
    },
    afterFetch: (data) => {
      data.map((item: any) => {
        if (!!item.video) {
          item.video = item.video.split(',')
        }
        return item;
      })
      return data;
    }
  },
  exportConfig: {
    name: "操作记录",
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext

const handleOk = (e) => {
  console.log(e)
}

const previewVideo = (list) => {
  openVideo(true, {
    list: list
  })
}

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}
/**
  * 详情
 */
function handleDetail(record: Recordable) {
  openDescInfo(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record: { id: any; }) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
  * 重推数据上链
  */
async function handleRepush(record: { id: any; }) {
  await repush({ id: record.id }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
   * 操作栏
   */
function getTableAction(record: any) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    }
  ]
}

/**
* 下拉操作栏
*/
function getDropDownAction(record: any) {
  return [
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      }
    }, {
      label: '重推上链',
      popConfirm: {
        title: '是否确认重新推送上链请求',
        confirm: handleRepush.bind(null, record),
      }
    }
  ]
}



</script>

<style scoped></style>
